package com.haierCamera.customapplication.ui.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.haierCamera.customapplication.BaseFragment;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.api.vo.GetUserInfoResponse;
import com.haierCamera.customapplication.databinding.HzklFragmentUserLoginBinding;
import com.haierCamera.customapplication.ui.WebViewPublicActivity;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.main.HZKLCameraDeviceListActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLBlankActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLRepairActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserAboutActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserAddressActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserFeedBackActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserInfoActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserSettingActivity;
import com.haierCamera.customapplication.ui.user.send.HZKLUserMsgActivity;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserLoginFragment extends BaseFragment {

    //    @Inject
//    ViewModelProvider.Factory factory;
    @Inject
    Lazy<HttpErrorProcess> httpErrorProcessLazy;
    @Inject
    UserRepo repo;
    HzklFragmentUserLoginBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.hzkl_fragment_user_login, container, false);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        binding.userAbout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserAboutActivity.class)));
//        binding.userAddressLayout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserAddressActivity.class)));
        binding.userInfo.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserInfoActivity.class)));
//        binding.userSettingLayout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserSettingActivity.class)));
//        binding.userAboutLayout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserAboutActivity.class)));
        binding.userIcon.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserInfoActivity.class)));
        binding.layoutSetting.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserSettingActivity.class)));
        binding.layoutHelpFeedback.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserFeedBackActivity.class)));
//        binding.layoutAgreement.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserAboutActivity.class)));

        binding.layoutAgreement.setOnClickListener(view -> startActivity(new Intent(getActivity(), WebViewPublicActivity.class)
                .putExtra("title", "隐私政策").putExtra("url", "http://edu-wc.nullehome.com/servicePotocol/#/")
        ));

        binding.ivWiki.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "产品百科"));
        });
        binding.ivIntelligent.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "全屋智能定制"));
        });
        binding.ivSelectContent.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "精选内容"));
        });
        binding.ivHotActivity.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "热门活动"));
        });
        binding.layoutBluetooth.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "蓝牙网关"));
        });
        binding.layoutOtherDevice.setOnClickListener((v) -> {
            startActivity(new Intent(getActivity()
                    , HZKLBlankActivity.class)
                    .putExtra("title", "其他平台设备"));
        });

        binding.layoutMsg.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserMsgActivity.class)));
//        binding.userDeviceLayout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLCameraDeviceListActivity.class)));
//
//        binding.userYun.setOnClickListener(view -> Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_SHORT).show());
//        binding.userShare.setOnClickListener(view -> Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_SHORT).show());
//        binding.userPhoneIcon.setOnClickListener(view -> Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_SHORT).show());
//        binding.userAiFace.setOnClickListener(view -> Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_SHORT).show());
//        binding.userAiAlert.setOnClickListener(view -> Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_SHORT).show());

        if (Business.getInstance().getmDeviceList() != null
                && Business.getInstance().getmDeviceList().size() != 0) {
            binding.tvDeviceNum.setText("" + Business.getInstance().getmDeviceList().size());
        }

    }

    @Override
    public void onResume() {
        super.onResume();

        binding.phone.setText(SharedPreferencesUtils.getString(Constants.SP_USER_PHONE, ""));
        if (!SharedPreferencesUtils.getString(Constants.SP_USER_HEAD_URL, "").equals("")) {
            Glide.with(this)
                    .load(SharedPreferencesUtils.getString(Constants.SP_USER_HEAD_URL, ""))
                    .apply(new RequestOptions().error(R.drawable.hzkl_user_icon).circleCrop())
                    .into(binding.userIcon);
        }
        getUser();
    }

    private void getUser() {
        repo.getUserInfo().observe(this, getUserInfoResource -> {
            switch (getUserInfoResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    break;
                case SUCCESS:
                    GetUserInfoResponse data = getUserInfoResource.data;
                    assert data != null;
                    if (TextUtils.isEmpty(data.nickName)) {
                        binding.phone.setText(data.mobile);
                    } else {
                        binding.phone.setText(data.nickName);
                    }
                    Glide.with(this).load(data.headUrl).apply(new RequestOptions().error(R.drawable.hzkl_user_icon).circleCrop()).into(binding.userIcon);
                    break;
            }
        });
    }
}
